package logic

import (
	"context"
	"lucky_srv/internal/svc"
	"lucky_srv/lucky"
	"lucky_srv/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchGoodsLogic {
	return &SearchGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchGoodsLogic) SearchGoods(in *lucky.SearchGoodsRequest) (*lucky.SearchGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var g models.Goods
	var listGs []*lucky.GoodsList

	goods, err := g.SearchGoods(in.Title, in.SortWord, in.Taste, in.OrderType,
		int(in.Page), int(in.PageSize), float64(in.MinPrice), float64(in.MaxPrice))
	if err != nil {
		return nil, err
	}

	for _, gs := range goods {
		list := lucky.GoodsList{
			Title:      gs.Title,
			Taste:      gs.Taste,
			Ingredient: gs.Ingredient,
			Price:      float32(gs.Price),
			Stock:      gs.Stock,
			ImageUrl:   gs.ImageUrl,
			Likes:      gs.Likes,
		}
		listGs = append(listGs, &list)
	}

	return &lucky.SearchGoodsResponse{
		List: listGs,
	}, nil
}
